package com.besttone.action;

import com.besttone.bean.BatchImportDetailBean;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.MD5Util;
import com.besttone.util.Sms2SolrUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocumentList;

import java.util.List;

/**
 * 组合查询信息
 * 
 * 测试案例：http://localhost:9090/signalAndsms_2_solr/sms/querySms
 * 
 * @author zhenzhen
 * 
 */
public class SmsAction extends Controller {
    private static Log _log = LogFactory.getLog(SmsAction.class);

    /**
     * 查询挂机短信列表
	 * 账号:百事通加盟=bstjm
     */
    @SuppressWarnings("rawtypes")
    public void querySms() {
		String streaming_no = getPara("streaming_no");
		String sp_account = getPara("sp_account");
		String sendPwd = getPara("sendPwd");//密码：MD5(streaming_no&sp_account&"sms")
	    String send_number = getPara("send_number");// 发送号码
	    String target_number = getPara("target_number");// 接收号码
	    String content_key_words = getPara("content_key_words");// 短信内容关键词
	    String call_status = getPara("call_status");// 通话状态
	    String send_time_from = getPara("send_time_from");// 发送时间开始：按天
	    String send_time_to = getPara("send_time_to");// 发送时间结束：按天
	    String mt_status = getPara("mt_status");// 下发状态
	    String template_code = getPara("template_code");
	    String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
		: getPara("current_page");// 当前页数
	    String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
		: getPara("per_page_size");// 每页条数，默认10
		StringBuffer pwdBuf=new StringBuffer();
		String nowpwd=MD5Util.md5(pwdBuf.append(streaming_no).append("&").append(sp_account)
				.append("&").append("sms").toString());
	_log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
		+ sp_account + ",send_number=" + send_number
		+ ",target_number=" + target_number + ",content_key_words="
		+ content_key_words + ",call_status=" + call_status
		+ ",send_time_from=" + send_time_from + ",send_time_to="
		+ send_time_to + ",mt_status=" + mt_status + ",per_page_size="
		+ per_page_size + ",template_code=" + template_code+",sendPwd="+sendPwd+",nowpwd="+nowpwd);
		/**if(!nowpwd.equals(sendPwd)){
			setAttr("result_code",
					EnumAPIResultCode.UNAUTHORIZED_BUSINESS.getValue());
			setAttr("result_msg", "鉴权失败:密码错误");
			renderJson();
			return;
		}
		if (StringUtils.isBlank(sp_account)) {
			setAttr("result_code",
					EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
			setAttr("result_msg", "账号不能为空");//账号为空会躲避密码验证
			renderJson();
			return;
		}**/
	if (StringUtils.isBlank(send_number)
		&& StringUtils.isBlank(template_code)) {
	    setAttr("result_code",
		    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
	    setAttr("result_msg", "发送号码或发送模板不能都为空");
	    renderJson();
	    return;
	}
	StringBuffer strBuf = new StringBuffer();
		strBuf.append("(send_parent_account:bstjm_gj OR send_parent_account:new_sms_plat) AND ");//default
	if (StringUtils.isNotBlank(send_number)
		&& StringUtils.isNotBlank(template_code)) {
	    strBuf.append("send_number:" + send_number);
		template_code = StringUtils.trim(template_code);
	    strBuf.append(" AND (template_code:");
	    strBuf.append(template_code);
	    strBuf.append(") ");
	}
	if (StringUtils.isNotBlank(send_number)) {
	    strBuf.append("send_number:" + send_number);
	}
	if (StringUtils.isNotBlank(template_code)) {
	    strBuf.append("template_code:" + template_code);
	}

	if (StringUtils.isNotBlank(template_code)) {
		template_code = StringUtils.trim(template_code);
	    strBuf.append(" AND (template_code:");
	    strBuf.append(template_code);
	    strBuf.append(") ");
	}
	if (StringUtils.isNotBlank(target_number)) {
	    target_number = StringUtils.trim(target_number);
	    strBuf.append(" AND (target_number:");
	    strBuf.append(target_number);
	    strBuf.append(") ");
	}
	if (StringUtils.isNotBlank(content_key_words)) {
	    content_key_words = StringUtils.trim(content_key_words);
	    strBuf.append(" AND (content_key_words:");
	    strBuf.append(content_key_words);
	    strBuf.append(") ");
	}
	if (StringUtils.isNotBlank(call_status)) {
	    call_status = StringUtils.trim(call_status);
	    strBuf.append(" AND (call_status:");
	    strBuf.append(call_status);
	    strBuf.append(") ");
	}
	// 类型是string,只能精确到天；如果需要精确到秒，需要solr端修改成date类型
	if (StringUtils.isNotBlank(send_time_from)) {
	    send_time_from = StringUtils.trim(send_time_from);
	    strBuf.append(" AND (send_time:[");
	    strBuf.append(send_time_from);
	    strBuf.append(" TO ");
	    if (StringUtils.isNotBlank(send_time_to)) {
		strBuf.append(StringUtils.trim(send_time_to));
	    } else {
		strBuf.append("*");
	    }
	    strBuf.append("]) ");
	} else if (StringUtils.isNotBlank(send_time_to)) {
	    send_time_to = StringUtils.trim(send_time_to);
	    strBuf.append(" AND (send_time:[* TO ");
	    strBuf.append(StringUtils.trim(send_time_to));
	    strBuf.append("]) ");
	}
	if (StringUtils.isNotBlank(mt_status)) {
	    mt_status = StringUtils.trim(mt_status);
	    strBuf.append(" AND (mt_status:");
	    strBuf.append(mt_status);
	    strBuf.append(") ");
	}

	String querySql = strBuf.toString();
	_log.info("solr查询语句:" + querySql);
	try {
	    setAttr("streaming_no", streaming_no);
	    SolrDocumentList docList = Sms2SolrUtil.search(querySql,
		    Integer.valueOf(current_page),
		    Integer.valueOf(per_page_size));
		setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
		List list = SolrObject.toBeanList(docList,
				BatchImportDetailBean.class);
		setAttr("result_msg", list);// 返回详情数据
		setAttr("result_num", docList.getNumFound());
	} catch (Exception e) {
		_log.info("solr查询异常:" + e);
		setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
		setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
	}
	renderJson();
	return;
    }
}
